package com.yellowpage.omc.o2o.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.nsp.service.NspLoggerService;
import com.yellowpage.omc.o2o.data.Channel;
import com.yellowpage.omc.o2o.service.ChannelService;

public class ChannelAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_O2OSTATISTICS = "O2OStatistics";
	public String RESULT_O2OLOGERSUM = "O2OLogerSum";
	
	
	

	private Configuration configuration;
	private NspLoggerService nspLoggerService;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<Channel> channelList = new ArrayList<Channel>();
	private List<Map> resultList = new ArrayList<Map>();

	private Channel channel;
	private ChannelService channelService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_channelId;
	private String query_lastOrder;
	private String query_url;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_note;
	private String query_name;
	private String query_status;
	private String query_logId;
	private String query_requestTime;
	private String query_requestTime_end;
	private String query_requestInfo;
	private String query_requestType;
	private String query_requestIp;
	private String query_responseData;
	private String query_responseInfo;
	private String query_id;
	private String query_responseCode;
	private String query_requestType_seach;
	
	private String query_logSumId;
	private String query_serverId;
	private String query_logFileName;
	private String query_logTime;
	private String query_requestContent;
	private String query_responseContent;

	private String query_count;


	private Map requestTypeMap = new HashMap();
	private Map responseCodeMap = new HashMap();
	
	public void prepare() {
		super.prepare();
		responseCodeMap=nspLoggerService.getresponseCodeMap();
		requestTypeMap=nspLoggerService.getrequestTypeMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.channel = channelService
					.getChannel(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("channelId", query_channelId);
		queryMap.put("lastOrder", query_lastOrder);
		queryMap.put("url", query_url);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("lastUpdateUser", query_lastUpdateUser);
		queryMap.put("lastUpdateTime", query_lastUpdateTime);
		queryMap.put("note", query_note);
		queryMap.put("name", query_name);
		queryMap.put("status", query_status);

		resultList = channelService.queryChannelByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		channel.setCreateUser(Integer.valueOf(loginUser.getUserId().toString()));
		channel.setCreateTime(new Date());
		channelService.saveChannel(channel);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (channel.getUrl().length() == 0) {
			this.addActionError("频道URL 不能为空 !");
		}
		
		if (channel.getLastOrder() == null) {
			this.addActionError("排序值 不能为空 !");
		}
		if (channel.getName().length() == 0) {
			this.addActionError("频道名称 不能为空 !");
		}
		

		this.setValidatorResult("/omc/o2o/ChannelAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		channel = channelService.getChannel(id);

		return RESULT_EDIT;
	}

	public String update() {
		channel.setLastUpdateUser(Integer.valueOf(loginUser.getUserId().toString()));
		channel.setLastUpdateTime(new Date());
		channelService.updateChannel(channel);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (channel.getUrl().length() == 0) {
			this.addActionError("频道URL 不能为空 !");
		}
		if (channel.getLastOrder() == null) {
			this.addActionError("排序值 不能为空 !");
		}
		if (channel.getName().length() == 0) {
			this.addActionError("频道名称 不能为空 !");
		}
		this.setValidatorResult("/omc/o2o/ChannelUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.channelService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突?删除失败 !");
		}

		return queryByPage();
	}
	/*o2o 日志统计*/
	public String getLogerStatistics(){
		
		
		return RESULT_O2OSTATISTICS;
	}
	
	public String getLogerStatisticsQuery(){
		Map queryMap = new HashMap();	
		queryMap.put("logId", query_logId);
		queryMap.put("requestTime", query_requestTime);
		queryMap.put("requestTimeEnd", query_requestTime_end);
		queryMap.put("requestInfo", query_requestInfo);
		queryMap.put("requestType", query_requestType);
		queryMap.put("requestIp", query_requestIp);
		queryMap.put("responseData", query_responseData);
		queryMap.put("responseInfo", query_responseInfo);
		queryMap.put("id", query_id);
		queryMap.put("responseCode", query_responseCode);
		
		resultList = channelService.getO2OStatistics(queryMap);
		
		return RESULT_O2OSTATISTICS;
	}
	
	public String getO2OLoger(){
		
		
		return RESULT_O2OLOGERSUM;
	}
	public String getO2OLogerQuery(){
		initPageRender();
		Map queryMap = new HashMap();	
	    queryMap.put("logSumId", query_logSumId);
	    queryMap.put("serverId", query_serverId);
	    queryMap.put("logFileName", query_logFileName);
	    queryMap.put("logTime", query_logTime);
	    queryMap.put("requestType", query_requestType);
	    queryMap.put("requestContent", query_requestContent);
	    queryMap.put("responseContent", query_responseContent);
	    queryMap.put("createUser", query_createUser);
	    queryMap.put("createTime", query_createTime);
	    queryMap.put("note", query_note);
	    queryMap.put("responseCode", query_responseCode);
	    queryMap.put("count", query_count);
	    queryMap.put("status", query_status);
		
		resultList = channelService.getO2OLogerSum(queryMap, pageRender);
		setPageRenderToRequest();
		return RESULT_O2OLOGERSUM;
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public List<Channel> getChannelList() {
		return channelList;
	}

	public void setChannelList(List<Channel> channelList) {
		this.channelList = channelList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_channelId() {
		return this.query_channelId;
	}

	public void setQuery_channelId(String channelId) {
		this.query_channelId = channelId;
	}

	public String getQuery_lastOrder() {
		return this.query_lastOrder;
	}

	public void setQuery_lastOrder(String lastOrder) {
		this.query_lastOrder = lastOrder;
	}

	public String getQuery_url() {
		return this.query_url;
	}

	public void setQuery_url(String url) {
		this.query_url = url;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public NspLoggerService getNspLoggerService() {
		return nspLoggerService;
	}

	public void setNspLoggerService(NspLoggerService nspLoggerService) {
		this.nspLoggerService = nspLoggerService;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public Map getResponseCodeMap() {
		return responseCodeMap;
	}

	public void setResponseCodeMap(Map responseCodeMap) {
		this.responseCodeMap = responseCodeMap;
	}

	public String getQuery_logId() {
		return query_logId;
	}

	public void setQuery_logId(String query_logId) {
		this.query_logId = query_logId;
	}

	public String getQuery_requestTime() {
		return query_requestTime;
	}

	public void setQuery_requestTime(String query_requestTime) {
		this.query_requestTime = query_requestTime;
	}

	public String getQuery_requestTime_end() {
		return query_requestTime_end;
	}

	public void setQuery_requestTime_end(String query_requestTime_end) {
		this.query_requestTime_end = query_requestTime_end;
	}

	public String getQuery_requestInfo() {
		return query_requestInfo;
	}

	public void setQuery_requestInfo(String query_requestInfo) {
		this.query_requestInfo = query_requestInfo;
	}

	public String getQuery_requestType() {
		return query_requestType;
	}

	public void setQuery_requestType(String query_requestType) {
		this.query_requestType = query_requestType;
	}

	public String getQuery_requestIp() {
		return query_requestIp;
	}

	public void setQuery_requestIp(String query_requestIp) {
		this.query_requestIp = query_requestIp;
	}

	public String getQuery_responseData() {
		return query_responseData;
	}

	public void setQuery_responseData(String query_responseData) {
		this.query_responseData = query_responseData;
	}

	public String getQuery_responseInfo() {
		return query_responseInfo;
	}

	public void setQuery_responseInfo(String query_responseInfo) {
		this.query_responseInfo = query_responseInfo;
	}

	public String getQuery_id() {
		return query_id;
	}

	public void setQuery_id(String query_id) {
		this.query_id = query_id;
	}

	public String getQuery_responseCode() {
		return query_responseCode;
	}

	public void setQuery_responseCode(String query_responseCode) {
		this.query_responseCode = query_responseCode;
	}

	public String getQuery_requestType_seach() {
		return query_requestType_seach;
	}

	public void setQuery_requestType_seach(String query_requestType_seach) {
		this.query_requestType_seach = query_requestType_seach;
	}

	public String getQuery_logSumId() {
		return query_logSumId;
	}

	public void setQuery_logSumId(String query_logSumId) {
		this.query_logSumId = query_logSumId;
	}

	public String getQuery_serverId() {
		return query_serverId;
	}

	public void setQuery_serverId(String query_serverId) {
		this.query_serverId = query_serverId;
	}

	public String getQuery_logFileName() {
		return query_logFileName;
	}

	public void setQuery_logFileName(String query_logFileName) {
		this.query_logFileName = query_logFileName;
	}

	public String getQuery_logTime() {
		return query_logTime;
	}

	public void setQuery_logTime(String query_logTime) {
		this.query_logTime = query_logTime;
	}

	public String getQuery_requestContent() {
		return query_requestContent;
	}

	public void setQuery_requestContent(String query_requestContent) {
		this.query_requestContent = query_requestContent;
	}

	public String getQuery_responseContent() {
		return query_responseContent;
	}

	public void setQuery_responseContent(String query_responseContent) {
		this.query_responseContent = query_responseContent;
	}

	public String getQuery_count() {
		return query_count;
	}

	public void setQuery_count(String query_count) {
		this.query_count = query_count;
	}

}
